package com.aaa.config;

import com.aaa.realm.MyShiroRealm;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfigration {

    //核心拦截器
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager sm){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //告诉拦截器我们的安全管理器是谁
        shiroFilterFactoryBean.setSecurityManager(sm);
        //告诉shiro拦截的请求是那些
        Map<String, String> filterMap = new HashMap<String, String>();
        filterMap.put("/**","anon");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        return shiroFilterFactoryBean;
    }

    //安全管理器
    @Bean
    public DefaultWebSecurityManager securityManager(MyShiroRealm r){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        // 告诉securityManager 授权 认证的时候 匹配不匹配的realm该找谁？
        defaultWebSecurityManager.setRealm(r);
        return defaultWebSecurityManager;
    }

    //自定义领域
    @Bean
    public MyShiroRealm realm(CredentialsMatcher matcher){
        MyShiroRealm myShiroRealm = new MyShiroRealm();
        //设置realm的加密匹配器
        myShiroRealm.setCredentialsMatcher(matcher);
        return myShiroRealm;
    }

    //加密匹配器
    @Bean
    public CredentialsMatcher matcher(){
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //加密方式
        hashedCredentialsMatcher.setHashAlgorithmName("SHA-512");
        //加密次数
        hashedCredentialsMatcher.setHashIterations(1024);
        return hashedCredentialsMatcher;
    }


}
